var t = new (require("../../libs/qqmap-wx-jssdk.min.js"))({
    key: "NEHBZ-IP7R4-WCAUL-DC5EM-2O652-MVBAP"
}), e = require("../../utils/config.js");

Page({
    data: {
        isHideMore: !1,
        formSettingId: "",
        list: [],
        formId: ""
    },
    onLoad: function(t) {
        t.formId ? (this.setData({
            formId: t.formId,
            isHideMore: !0
        }), wx.setNavigationBarTitle({
            title: t.name
        }), this.loadDetail()) : (this.setData({
            formSettingId: t.id
        }), wx.setNavigationBarTitle({
            title: t.name
        }), this.loadDataSource());
    },
    getAddress: function(e) {
        var a = this;
        wx.getLocation({
            type: "gcj02",
            success: function(s) {
                t.reverseGeocoder({
                    location: {
                        latitude: s.latitude,
                        longitude: s.longitude
                    },
                    success: function(t) {
                        console.log(t), a.setKeyValue(e, t.result.formatted_addresses.recommend + "|" + t.result.location.lng + "|" + t.result.location.lat);
                    },
                    fail: function(t) {
                        console.log(t);
                    }
                });
            }
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    loadDataSource: function() {
        var t = this;
        wx.showLoading({
            mask: !0
        }), wx.request({
            url: e.urlList.API_FORM_SETTING_DETAIL,
            method: "GET",
            data: {
                id: t.data.formSettingId
            },
            success: function(e) {
                wx.hideLoading(), 200 == e.data.status ? (e.data.data.forEach(function(e, a) {
                    t.data.list.push({
                        key: e.id,
                        value: "",
                        isMust: e.isMust,
                        name: e.name,
                        id: e.id,
                        settingType: e.settingType
                    }), 4 == e.settingType && t.getAddress(e.id);
                }), t.setData({
                    list: t.data.list
                })) : wx.showToast({
                    title: e.data.message,
                    icon: "none"
                });
            }
        });
    },
    getInputValue: function(t) {
        var e = t.currentTarget.dataset.key, a = t.detail.value;
        this.setKeyValue(e, a);
    },
    setKeyValue: function(t, e) {
        this.data.list.forEach(function(a, s) {
            a.key == t && (a.value = e);
        }), this.setData({
            list: this.data.list
        });
    },
    previewImage: function(t) {
        console.log(t.target.dataset.src);
        t.target.dataset.src && wx.previewImage({
            current: "https://www.xiangyaowant.com/api/Tools/GetFile/" + t.target.dataset.src,
            urls: [ "https://www.xiangyaowant.com/api/Tools/GetFile/" + t.target.dataset.src ]
        });
    },
    chooseImage: function(t) {
        var a = this, s = t.currentTarget.dataset.key;
        wx.chooseImage({
            count: 1,
            success: function(t) {
                wx.showLoading({
                    mask: !0
                }), wx.uploadFile({
                    url: e.urlList.API_TOOLS_UPLOAD_FILE,
                    filePath: t.tempFilePaths[0],
                    name: "file",
                    success: function(t) {
                        wx.hideLoading();
                        var e = JSON.parse(t.data);
                        200 == e.status ? a.setKeyValue(s, e.data.fileId) : (wx.hideLoading(), wx.showToast({
                            title: t.data.message,
                            icon: "none"
                        }));
                    }
                });
            }
        });
    },
    apply: function() {
        for (var t = this, a = wx.getStorageSync("userId"), s = wx.getStorageSync("serviceProviderId"), i = 0; i < t.data.list.length; i++) {
            var o = t.data.list[i];
            if (o.isMust && (!o.value || "" == o.value)) return console.log(o), void wx.showToast({
                title: "请输入" + o.name,
                icon: "none"
            });
        }
        wx.showLoading({
            mask: !0
        }), wx.request({
            url: e.urlList.API_FORM_SAVE,
            method: "POST",
            data: {
                formSettingId: t.data.formSettingId,
                userId: a,
                serviceProviderId: s,
                details: t.data.list
            },
            success: function(t) {
                wx.hideLoading(), 200 == t.data.status ? wx.navigateBack({
                    success: function() {
                        wx.showToast({
                            title: "提交成功"
                        });
                    }
                }) : 415 == t.data.status ? wx.showModal({
                    title: "系统提示",
                    content: t.data.message,
                    cancelText: "下次再说",
                    confirmText: "立即完善",
                    success: function(t) {
                        t.confirm && wx.navigateTo({
                            url: "/pages/my/editBank"
                        });
                    }
                }) : 416 == t.data.status ? wx.showModal({
                    title: "系统提示",
                    content: t.data.message,
                    cancelText: "下次再说",
                    confirmText: "实名认证",
                    success: function(t) {
                        t.confirm && wx.navigateTo({
                            url: "/pages/my/realNameAuthentication"
                        });
                    }
                }) : wx.showToast({
                    title: t.data.message,
                    icon: "none"
                });
            }
        });
    },
    goToHistory: function() {
        wx.navigateTo({
            url: "formList?id=" + this.data.formSettingId
        });
    },
    loadDetail: function() {
        var t = this;
        wx.showLoading({
            mask: !0
        }), wx.request({
            url: e.urlList.API_FORM_DETAIL,
            data: {
                id: t.data.formId
            },
            method: "GET",
            success: function(e) {
                if (wx.hideLoading(), 200 == e.data.status) {
                    e.data.data.forEach(function(e, a) {
                        t.data.list.push({
                            key: e.formSettingDetailId,
                            value: e.formSettingDetailValue,
                            isMust: e.isMust,
                            name: e.formSettingDetailName,
                            id: e.formSettingDetailId,
                            settingType: e.settingType
                        });
                    }), t.setData({
                        list: t.data.list
                    });
                } else wx.showToast({
                    title: e.data.message,
                    icon: "none"
                });
            }
        });
    }
});